package com.sec.android.app.myfiles.facade.cmd;

import android.content.Context;
import android.text.TextUtils;
import android.widget.Toast;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.feature.KnoxMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.local.category.CategoryFileRecord;
import com.sec.android.app.myfiles.util.FileUtils;
import com.sec.android.app.myfiles.util.SemFwWrapper;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.io.File;
import java.util.ArrayList;

public class MoveKnoxModeCmd extends SimpleCommand {

    private KnoxMgr mKnoxModeMgr;
    private Context mContext;
    private AbsMyFilesFragment mFragment;
    private ArrayList<FileRecord> mSrcList;
    private String mDestPath;
    private int mType;

    @SuppressWarnings("unchecked")
    @Override
    public void execute(INotification notification) {

        Object[] params = (Object[]) notification.getBody();

        mContext = (Context) params[0];
        mFragment = (AbsMyFilesFragment) params[1];
        mSrcList = (ArrayList<FileRecord>) params[2];
        mDestPath = (String) params[3];
        mType = (int) params[4];

        mKnoxModeMgr = KnoxMgr.getInstance(mContext);

        if (mSrcList != null) {
            if (mSrcList.get(0) instanceof CategoryFileRecord) {
                mSrcList = FileUtils.getCategoryFolderRecordList(null, mSrcList);
            }

            if (mSrcList.size() > AppConstants.SELECT_LIMIT_COUNT || hasChildDirCount(mSrcList) > AppConstants.SELECT_LIMIT_COUNT) {
                String warningMsg = mContext.getString(R.string.knox_move_warning, AppConstants.SELECT_LIMIT_COUNT);
                Toast.makeText(mContext, warningMsg, Toast.LENGTH_LONG).show();
            } else {
                mKnoxModeMgr.startMoveFiles(mSrcList, mDestPath, mType);
                mFragment.finishActionMode();
            }
        } else {
            Log.e(this, "execute - src list is null");
        }
    }

    private int hasChildDirCount(ArrayList<FileRecord> selectedItem) {
        int count = 0;

        if (selectedItem != null && !selectedItem.isEmpty()) {
            for (FileRecord record : selectedItem) {
                if (record.isDirectory()) {
                    String path = record.getFullPath();
                    if (!TextUtils.isEmpty(path)) {
                        count = getLocalDirInfo(path, count);
                        if (count > AppConstants.SELECT_LIMIT_COUNT) {
                            break;
                        }
                    }
                } else {
                    count++;
                }
            }
        }

        return count;
    }

    private int getLocalDirInfo(String directoryPath, int count) {
        File[] files = SemFwWrapper.file(directoryPath).listFiles();

        if (files != null) {
            for (File item : files) {
                if (item.isDirectory()) {
                    String path = item.getAbsolutePath();
                    if (!TextUtils.isEmpty(path)) {
                        count = getLocalDirInfo(path, count);
                    }
                } else {
                    count++;
                }

                if (count > AppConstants.SELECT_LIMIT_COUNT) {
                    break;
                }
            }
        } else {
            Log.d(this, "getLocalDirInfo - files is null");
        }

        return count;
    }
}